import Vue from 'vue'
import axios from 'axios'
import http from '@/utils/httpRequest'

/**
 * 通过名称获取 字典详情列表
 * @param {string} name
 */
export function getSysDictByName (name) {
  return http({
    url: http.adornUrl('/sys/dict/select'),
    method: 'post',
    params: {
      name: name
    }
  })
}

/**
 * 获取登录用户的信息
 */
export function getLoginUserInfo () {
  return http({
    url: http.adornUrl('/sys/user/info'),
    method: 'get',
    params: http.adornParams()
  })
}

/**
 * 获取区域树
 * @param {string} name
 */
export function getAreaNav (lev = 3) {
  return http({
    url: http.adornUrl('/common/area/nav'),
    method: 'get',
    params: http.adornParams({ lev: lev })
  })
}

/**
 * 通过文件id { 1,2,3 } 获取文件信息列表
 * @param {string} name
 */
export function getFileList (ids) {
  let fileIdList
  if (Array.isArray(ids)) {
    fileIdList = ids
  } else if (typeof (ids) === 'string') {
    fileIdList = ids.split(',')
  } else if (typeof (ids) === 'number') {
    fileIdList = [ids]
  } else {
    throw new Error('文件id不对')
  }
  return http({
    url: http.adornUrl('/sys/toolLocalStorage/info'),
    method: 'post',
    data: fileIdList
  })
}

/**
 * 导出文件。
 *
 * @param {Object} options
 */
export function exportFile (options) {
  return new Promise(resolve => {
    axios({
      url: http.adornUrl(options.url),
      method: options.method || 'post',
      timeout: 30000,
      headers: {
        token: Vue.cookie.get('token')
      },
      responseType: 'arraybuffer', // 'application/vnd.ms-excel', // 'blob',
      params: options.params
    }).then(res => {
      const blob = new Blob([res.data])
      let fileName = res.headers['content-disposition'].split('=')[1]
      fileName = decodeURI(fileName)
      if ('download' in document.createElement('a')) { // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
      } else { // IE10+下载
        navigator.msSaveBlob(blob, fileName)
      }
      resolve()
    })
  })
}

/**
 * 下载文件。
 *
 * @param {Object} options
 */
export function downloadFile (fileId, fileName) {
  return new Promise(function (resolve, reject) {
    axios({
      url: http.adornUrl(`/sys/toolLocalStorage/download/${fileId}`),
      methods: 'get',
      responseType: 'blob',
      headers: {
        token: Vue.cookie.get('token')
      }
    }).then((res) => {
      const blob = new Blob([res.data])
      let name = fileName
      if (!name) {
        name = res.headers['content-disposition'].split('=')[1]
        name = decodeURI(name)
      }
      if ('download' in document.createElement('a')) { // 非IE下载
        const elink = document.createElement('a')
        elink.download = name
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
      } else { // IE10+下载
        navigator.msSaveBlob(blob, fileName)
      }
      resolve()
    })
  })
}

export function downloadMould (url, fileId, fileName) {
  axios({
    url: http.adornUrl(url),
    method: 'post',
    responseType: 'blob',
    data: fileId,
    headers: {
      token: Vue.cookie.get('token')
    }
  }).then((res) => {
    const blob = new Blob([res.data])
    let name = fileName
    if (name) {
      name = res.headers['content-disposition'].split('=')[1]
      name = decodeURI(name)
    }
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = name
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, fileName)
    }
  })
}


export function getPoints () {
  return http({
    url: http.adornUrl('/bas/point/search/list'),
    method: 'post',
    data: http.adornParams({
      page: 1,
      limit: 20000
    })
  })
}
